package com.molin.exceptionTest;

import java.util.Scanner;

public class Demo8 {
    public static void main(String[] args) {
        //age 18-25,while(true)
        Student st = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("input Student's name: ");
        String s = sc.nextLine();
        while (true) {
            try {
                System.out.println("input Student's age: ");
                int age = Integer.parseInt(sc.nextLine());
                st.setAge(age);
                st.setName(s);
                break;
            } catch (NumberFormatException ex) {
                System.out.println("请输入一个整数！");
            } catch (AgeOutOfBoundsExcetion ex) {
                System.out.println("输入的年龄不合法");
                continue;
            }
        }
        System.out.println(st.getName() + " " + st.getAge());
    }
}

class Student {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 18 && age <= 25) {
            this.age = age;
        } else {
//            throw new RuntimeException();
        throw new AgeOutOfBoundsExcetion("年龄超出了范围");
        }


    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;

    public Student() {
    }

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
class AgeOutOfBoundsExcetion extends RuntimeException{
    public AgeOutOfBoundsExcetion() {
    }

    public AgeOutOfBoundsExcetion(String message) {
        super(message);
    }
}